<?php
namespace app\index\controller;
use think\Db;
//认证租客数据接口
class Tenant extends Base{
	//申请成为业主
    public function apply_ower(){
    	$user=$this->_user;
    	$param=$this->_data; 
    	$val=validate('Tenant');
		if(!$val->scene('apply_ower')->check($param)) return rel(-1,$val->getError());
		if($user['phone']!=$param['phone']) return rel(-1,'手机号与注册号码不匹配');
		
		$insert=[
			'uid' 	  => $user['id'],
			'head_img'=> $user['head_img'],
			'phone'   => $user['phone'],
			'nickname'=> $user['nickname'],
			'name'	  => $user['name'],
			'idcard'  => $user['idcard'],
			'ctime'   => time()
		];
		$rel=Db::name('applyOwer')->insert($insert);
		if(!$rel){
			return rel(-1,'申请失败，稍后重试');
		}else{
			return rel(1,'申请成功，等待回复');
		}
    }
    //返回当前房源的看房记录状态（申请看房前的准备工作）
    public function see_house_stat(){
    	$user =$this->_user;
    	$param=$this->_data;
    	$val=validate('Tenant');
		if(!$val->scene('see_house_stat')->check($param)) return rel(-1,$val->getError());
    	$house=$val->house;
    	if($house['selled']==2) return rel(-1,'该房源已出租');
    	
    	$time =time();
    	$year =!empty($param['year']) ? $param['year'] : date('Y',$time);
    	
    	$map=[
    		'house_id'	=>$param['house_id'],
    		'year'		=>$year,
    		'month'		=>$param['month'],
    		'day'		=>$param['day']
    	];
    	$see_house=Db::name('seeHouse')
    	->field('house_id,year,month,day,hour,minute,see_time')
    	->where($map)
    	->select();
    	if(!$see_house){
    		return rel(-1,'当前没有预约看房申请');
    	}else{
    		return rel(1,'获取预约看房数据成功',$see_house);	
    	}
    }
    //预约看房
    public function see_house(){
    	$user =$this->_user;
    	$param=$this->_data;
    	$val=validate('Tenant');
		if(!$val->scene('see_house')->check($param)) return rel(-1,$val->getError());
    	$house=$val->house;
    	if($house['selled']==2) return rel(-1,'该房源已出租');
    	
    	$time =time();
    	$year =!empty($param['year']) ? $param['year'] : date('Y',$time);
    	$phone=!empty($param['phone']) ? $param['phone'] : $user['phone']; 
    	//根据年月日 时分生成唯一的时间邮戳
    	$time_temp=strtotime($year.'-'.$param['month'].'-'.$param['day'].' '.$param['hour'].':'.$param['minute']);
    	$see_house=Db::name('seeHouse')->where(['house_id'=>$house['id'],'see_time'=>$time_temp])->find();
    	if($see_house) return rel(-1,'该时间段看房已被预约');
    	
    	$insert=[
    		'tenant_id' =>$user['id'],
    		'ower_id'	=>$house['uid'],
    		'house_id'	=>$house['id'],
    		'year'		=>$year,
    		'month'		=>$param['month'],
    		'day'		=>$param['day'],
    		'hour'		=>$param['hour'],
    		'minute'	=>$param['minute'],
    		'see_time'  =>$time_temp,
    		'phone'		=>$phone,
    		'remark'	=>!empty($param['remark']) ? $param['remark'] : '',
    		'ctime'		=>$time 
    	];
    	$id=Db::name('seeHouse')->insertGetId($insert);
    	if(!$id){
    		return rel(-1,'预约看房失败，稍后重试');
    	}else{
    		$message=controller('common/Message','Logic');
    		$message->put_message(1,$id,$house['uid'],$user['id']);	//插入预约看房的信息
    		return rel(1,'预约看房成功');
    	}
    }
    //故障维修
	public function fault(){
        $user=$this->_user;
        $param=$this->_data;
        if(!isset($param['decl']) || empty($param['decl'])) return rel(-1,'回复内容不能为空');
        if(!isset($param['house_id']) || empty($param['house_id'])) return rel(-1,'房源主键不能为空');
        $house=Db::name('house')->where('id',$param['house_id'])->find();
        if($house['del']==1 || $house['stat']==0) return rel(-1,'该房源已删除');
        
        $reply = [
            'uid'           =>$user['id'],
            'house_id'      =>$house['id'],
            'house_tit'		=>$house['tit'],
            'decl'          =>$param['decl'],
            'imgs'          =>!empty($param['imgs']) ? $param['imgs'] : '',
            'phone'         =>!empty($param['phone']) ? $param['phone'] : $user['phone'],
            'ctime'			=>time()
        ];
        $rsl = Db::name('fault')->insert($reply);
        if(!$rsl){
            return rel(-1,'提交失败，请稍后再试');
        }else{
            return rel(1,'提交成功');
        }
	}
	//发布话题 
    public  function put_topic(){
        $user=$this->_user;
        $param=$this->_data;
        if(!isset($param['tit']) || empty($param['tit'])) return rel(-1,'话题标题不能为空');
        if(!isset($param['topic']) || empty($param['topic'])) return rel(-1,'话题内容不能为空');
        $insert=[
            'uid'  => $user['id'],
            'tit'  => $param['tit'],
            'topic'=> $param['topic'],
            'ctime'=>time()
        ];
        $rel=Db::name('topic')->insert($insert);
        if(!$rel){
            return rel(-1,'发布话题失败，稍后重试');
        }else{
            return rel(1,'发布话题成功');
        }
    }
    //获取话题列表
    public function topic_list(){
        $param=$this->_data;
        $page=!empty($param['page']) ? $param['page'] : 1;
        
        $rel=Db::name('topic')->alias('t')
        ->field('t.id as topic_id,t.tit,t.topic,t.ctime,t.num,u.nickname,u.id as uid,u.head_img')
        ->join('users u','t.uid=u.id')
        ->where('t.del',0)
        ->page($page,10)
        ->select();
        
        if(!$rel){
            return rel(-1,'没有更多数据');
        }else{
            return rel(1,'获取话题数据成功',$rel);
        }
    }
    //获取单条话题详细数据
    public function topic(){
    	$param=$this->_data;
    	if(!isset($param['topic_id']) || empty($param['topic_id'])) return rel(-1,'话题主键不能为空');
        
        $rel=Db::name('topic')->alias('t')
        ->field('t.id as topic_id,t.tit,t.topic,t.ctime,t.num,u.nickname,u.id as uid,u.head_img')
        ->join('users u','t.uid=u.id')
        ->where(['t.del'=>0,'t.id'=>$param['topic_id']])
        ->find();
        
        if(!$rel){
            return rel(-1,'该话题不存在');
        }else{
        	Db::name('topic')->where('id',$param['topic_id'])->setInc('num');
            return rel(1,'获取话题数据成功',$rel);
        }
    }
    //获取话题的回复列表
    public function topic_remark(){
    	$param=$this->_data;
    	if(!isset($param['topic_id']) || empty($param['topic_id'])) return rel(-1,'话题主键不能为空');
    	$page = !empty($param['page']) ? $param['page'] : 1;
        
        $map['t.topic_id']=$param['topic_id'];
        $map['t.del']=0;
        
        $rel=Db::name('topicRemark')->alias('t')
        ->field('t.remark,t.ctime,u.nickname,u.id as uid,u.head_img')
        ->join('users u','t.uid=u.id')
        ->where($map)
        ->page($page,10)
        ->select();
        
        if(!$rel){
            return rel(-1,'没有更多数据');
        }else{
            return rel(1,'获取话题回复数据成功',$rel);
        }
    }
    //话题回复
    public function reply_topic(){
        $user=$this->_user;
        $param=$this->_data;
        if(!isset($param['topic_id']) || empty($param['topic_id'])) return rel(-1,'话题主键不能为空');
        if(!isset($param['remark']) || empty($param['remark'])) return rel(-1,'回复内容不能为空');
        
        $reply = [
            'uid'           =>$user['id'],
            'topic_id'      =>$param['topic_id'],
            'remark'        =>$param['remark'],
            'ctime'			=>time()
        ];
        $rsl = Db::name('topicRemark')->insert($reply);
        if(!$rsl){
            return rel(-1,'回复失败请稍后再试');
        }else{
            return rel(1,'回复成功');
        }
    }
    //获取租客的合同数据
	public function contract(){
		$user=$this->_user;
		$param=$this->_data;
		
		if(!empty($param['page'])){
			$rel=Db::name('contract')->alias('c')
			->field('c.*,u.name,u.phone,u.idcard,h.tit as house_tit')
			->join('users u','c.ower_id=u.id')
			->join('house h','c.house_id=h.id')
			->where('tenant_id',$user['id'])
			->page($param['page'],10)
			->select();
			if(!$rel){
				return rel(-1,'没有更多数据');
			}else{
				return rel(1,'获取数据成功',$rel);
			}	
		}else{
			$rel=Db::name('contract')->alias('c')
			->field('c.*,u.name,u.phone,u.idcard,h.tit as house_tit')
			->join('users u','c.ower_id=u.id')
			->join('house h','c.house_id=h.id')
			->where('tenant_id',$user['id'])
			->select();
			if(!$rel){
				return rel(-1,'没有更多数据');
			}else{
				return rel(1,'获取数据成功',$rel);
			}	
		}
	}
	//租客退房
	public function  retreat_house(){
		$user=$this->_user;
		$param=$this->_data;
		
		$val=validate('Tenant');
		if(!$val->scene('retreat_house')->check($param)) return rel(-1,$val->getError());
    	$contract=Db::name('contract')->where('id',$param['contract_id'])->find();
    	if(!$contract) return rel(-1,'无效的合同主键');
    	if($contract['stat']!=2) return rel(-1,'该合同处于非签署状态');
    	if($contract['tenant_id']!=$user['id']) return rel(-1,'您非该合同对象');
    	$message=controller('common/Message','Logic');
		$message->put_message(3,$contract['id'],$user['id'],$contract['ower_id']);
    	return rel(1,'发起退房申请成功');
	}
}
//Db::name()->whert()->order()
//->page(10,false,p['query']=>request()->param())
//
//gerLastLnsID
//getLastInsID